package gormx

import (
	"context"
	"gorm.io/gorm"
)

type (
	TxModel interface {
		Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
	}

	defaultTxModel struct {
		conn *gorm.DB
	}
)

func NewTxModel(db *gorm.DB) *defaultTxModel {
	return &defaultTxModel{
		conn: db,
	}
}

func (m *defaultTxModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
	return m.conn.WithContext(ctx).Transaction(fn)
}
